package control;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class Bet
 */
@WebServlet("/bet")
public class Bet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static int r;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Bet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Bet?money=10000&choice=up
		String choice = request.getParameter("choice");
		String money = request.getParameter("money");
		int m = Integer.parseInt(money);
		Me.money = Me.money - m;
		Random random = new Random();
		r = random.nextInt(11);
		
		String msg = "";
		if( choice == null ) {
			request.setAttribute("data2", null);
		}else if (choice.equals("up")) {
			if (r >= 6) {
				Me.money = (int) (Me.money + m * 2);
				msg = "WIN!!";
			} else {
				msg = "LOSE!!";
			}

		} else {
			if (r >= 6) {
				msg = "LOSE!!";
			} else {
				Me.money = (int) (Me.money + m * 2);
				msg = "WIN!!";
			}
		}
		
		request.setAttribute("msg", msg);
		request.setAttribute("num", r);
		request.setAttribute("data2", new Data2().cards ); 
		request.getRequestDispatcher("betting.jsp").forward(request,response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
